package DBLayer;
import ModelLayer.Tax;
import java.sql.*;
import java.util.ArrayList;

public class DBTax {
    // <editor-fold defaultstate="collapsed" desc="Attributes & Constructors">
    private Connection conn;

    public DBTax() {
        conn = SQLConnector.getInstance().getConnection();
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="SQL SELECT Methods">
    public Tax getTaxByID(int id) {
        return selectSingle("SELECT `id_tax`, `rate`, `name` FROM `ps_tax` LEFT JOIN `ps_tax_lang` USING (`id_tax`) WHERE (`id_lang` = " + Constants.DEFAULT_LANGUAGE + " OR `id_lang` IS NULL) AND `id_tax` = " + id + " LIMIT 0, 1;");
    }

    public ArrayList<Tax> getTaxList() {
        return selectMultiple("SELECT `id_tax`, `rate`, `name` FROM `ps_tax` LEFT JOIN `ps_tax_lang` USING (`id_tax`) WHERE `id_lang` = " + Constants.DEFAULT_LANGUAGE + " OR `id_lang` IS NULL ORDER BY `id_tax` ASC;");
    }

    private Tax selectSingle(String sqlStatement) {
        Statement stmt = null;
        ResultSet result = null;
        Tax tax = new Tax();

        try {
            stmt = conn.createStatement();
            result = stmt.executeQuery(sqlStatement);

            if (result.next()) {
                tax = buildTax(result);
            }
            System.out.println("Fetching Tax Object");
        } catch (SQLException e) {
            System.out.println("SQLException: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("VendorError: " + e.getErrorCode());
        } finally {
            if (result != null) {
                try {
                    result.close();
                } catch (SQLException e) {
                    System.out.println("SQLException: " + e.getMessage());
                    System.out.println("SQLState: " + e.getSQLState());
                    System.out.println("VendorError: " + e.getErrorCode());
                }
                result = null;
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    System.out.println("SQLException: " + e.getMessage());
                    System.out.println("SQLState: " + e.getSQLState());
                    System.out.println("VendorError: " + e.getErrorCode());
                }
                stmt = null;
            }
        }
        return tax;
    }

    private ArrayList<Tax> selectMultiple(String sqlStatement) {
        Statement stmt = null;
        ResultSet result = null;
        ArrayList<Tax> taxes = new ArrayList<Tax>();

        try {
            stmt = conn.createStatement();
            result = stmt.executeQuery(sqlStatement);

            while (result.next()) {
                Tax tax = new Tax();
                tax = buildTax(result);
                taxes.add(tax);
            }
            System.out.println("Fetching Tax List");
        } catch (SQLException e) {
            System.out.println("SQLException: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("VendorError: " + e.getErrorCode());
        } finally {
            if (result != null) {
                try {
                    result.close();
                } catch (SQLException e) {
                    System.out.println("SQLException: " + e.getMessage());
                    System.out.println("SQLState: " + e.getSQLState());
                    System.out.println("VendorError: " + e.getErrorCode());
                }
                result = null;
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    System.out.println("SQLException: " + e.getMessage());
                    System.out.println("SQLState: " + e.getSQLState());
                    System.out.println("VendorError: " + e.getErrorCode());
                }
                stmt = null;
            }
        }
        return taxes;
    }

    private Tax buildTax(ResultSet result) {
        Tax tax = new Tax();
        try {
            tax.setIDTax(result.getInt("id_tax"));
            tax.setRate(result.getFloat("rate"));
            tax.setName(result.getString("name"));

        } catch (Exception e) {
            System.out.println("Exception: " + e.getMessage());
        }
        return tax;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="SQL INSERT, UPDATE & DELETE Methods">
    public int addTax(Tax tax) {
        int returnID = 0;
        Statement stmt = null;
        ResultSet result = null;

        String query = "INSERT INTO `ps_tax_lang` (`id_tax`, `id_lang`, `name`) "
                + "VALUES (" + tax.getIDTax() + ", " + Constants.DEFAULT_LANGUAGE + ", '" + tax.getName() + "');";

        query += "INSERT INTO `ps_tax` (`id_tax`, `rate`) "
                + "VALUES (" + tax.getIDTax() + ", "
                + "VALUES (" + tax.getRate() + ");";
        try {
            stmt = conn.createStatement();
            stmt.executeUpdate(query, Statement.RETURN_GENERATED_KEYS);
            result = stmt.getGeneratedKeys();
            if (result.next()) {
                returnID = result.getInt(1);
            }
            System.out.println("Added Tax ID " + returnID);
        } catch (SQLException e) {
            System.out.println("SQLException: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("VendorError: " + e.getErrorCode());
        } finally {
            if (result != null) {
                try {
                    result.close();
                } catch (SQLException e) {
                    System.out.println("SQLException: " + e.getMessage());
                    System.out.println("SQLState: " + e.getSQLState());
                    System.out.println("VendorError: " + e.getErrorCode());
                }
                result = null;
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    System.out.println("SQLException: " + e.getMessage());
                    System.out.println("SQLState: " + e.getSQLState());
                    System.out.println("VendorError: " + e.getErrorCode());
                }
                stmt = null;
            }
        }
        return returnID;
    }

    public int updateTax(Tax tax, int id) {
        int returnID = 0;
        Statement stmt = null;
	String query = "UPDATE `ps_tax` SET "
                + "`id_tax`   = "  + tax.getIDTax() + ", "
                + "`rate`     = "  + tax.getRate() + " "
                + "WHERE `id_tax` = " + id + " LIMIT 1; ";

        query += "UPDATE `ps_tax_lang` SET "
                + "`id_tax`   = "  + tax.getIDTax() + ", "
                + "`name`         = "  + tax.getName() + " "
                + "WHERE `id_lang` = " + Constants.DEFAULT_LANGUAGE + " AND `id_tax` = " + id + " LIMIT 1;";

        try {
            stmt = conn.createStatement();
            stmt.executeUpdate(query);
            returnID = tax.getIDTax();
            System.out.println("Updated Tax ID " + id);
        } catch (SQLException e) {
            System.out.println("SQLException: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("VendorError: " + e.getErrorCode());
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    System.out.println("SQLException: " + e.getMessage());
                    System.out.println("SQLState: " + e.getSQLState());
                    System.out.println("VendorError: " + e.getErrorCode());
                }
                stmt = null;
            }
        }
        return returnID;
    }

    public void deleteTax(int id) {
        Statement stmt = null;

        try {
            stmt = conn.createStatement();
            stmt.executeUpdate("DELETE FROM `ps_tax` WHERE `id_tax` = " + id + " LIMIT 1; DELETE FROM `ps_tax_lang` WHERE `id_lang` = " + Constants.DEFAULT_LANGUAGE + " AND `id_tax` = " + id + " LIMIT 1;");
            System.out.println("Deleted Tax ID " + id);
        } catch (SQLException e) {
            System.out.println("SQLException: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("VendorError: " + e.getErrorCode());
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    System.out.println("SQLException: " + e.getMessage());
                    System.out.println("SQLState: " + e.getSQLState());
                    System.out.println("VendorError: " + e.getErrorCode());
                }
                stmt = null;
            }
        }
    }
    // </editor-fold>
}
